
#include "stdafx.h"
#include "HelperFunctions.h"
#include "MFCSubClasses.h"

//------------------------------------------------------------------------------------
// inline const char * ValidAttr(const char * val)
// {
//  if(val)
//   return val;
//  else
//   return "";
// }
//------------------------------------------------------------------------------------
void RecurseLoadProperty(CMFCPropertyGridProperty *prop,TiXmlElement *e)
{
	CString sName,sDesc,sType("0"),sValueType,sOpt,sOptions,sFormat;
	int iData = 0,iValueList = 0;
	variant_t sValue;
	TiXmlElement* elem = e->FirstChildElement();
	if(!elem) return;
	do
	{
	 CMFCPropertyGridProperty* pProp;
	 sType = elem->Attribute("type");
     iData = iValueList = 0;
     sName = elem->Attribute("name");
     iData = gSkinSystem->GetBindingValue(elem->Attribute("data"));
	 if(sType == "1")
	 {
	  elem->Attribute("valuelist",&iValueList);
      pProp = new CExMFCPropertyGridProperty(sName,iData,iValueList);
      iData = 0;
      elem->Attribute("disabled",&iData);
	  pProp->Enable(!iData);
	 }
	 else if(sType == "3")
	 {
      sDesc = elem->Attribute("desc");
      sValue = elem->Attribute("value");
      sValueType = elem->Attribute("vtype");
      sFormat = elem->Attribute("floatformat");
	  if(sValueType == "float") sValue.ChangeType(VT_R4);
      else if(sValueType == "integer") sValue.ChangeType(VT_INT);
      if(sFormat == _T("")) sFormat = _T("%.2f");
	  pProp = new CExMFCPropertyGridProperty(sName,sValue,sDesc,iData,sFormat);
	  sOptions = elem->Attribute("options");
	  int iStart = 0;
	  
	  sOpt = sOptions.Tokenize(_T(";"),iStart);
      while (sOpt != _T(""))
      {
	   pProp->AddOption(sOpt);
	   sOpt = sOptions.Tokenize(_T(";"),iStart);
      };  
	  iData = 0;
      elem->Attribute("disabled",&iData);
	  pProp->Enable(!iData);
	 }
	 else if(sType == "4")
	 {
      sDesc = elem->Attribute("desc");
	  sDesc ="";
      pProp = new CMFCPropertyGridColorProperty(sName,0,NULL,sDesc,iData);
	  ((CMFCPropertyGridColorProperty *)pProp)->EnableOtherButton(_T("Other"),true,true);

	  ((CMFCPropertyGridColorProperty *)pProp)->EnableAutomaticButton(_T("moren"), ::GetSysColor(COLOR_3DFACE));
      iData = 0;
      elem->Attribute("disabled",&iData);
	  pProp->Enable(!iData);
	 }
	 else if(sType == "5")
	 {
      sDesc = elem->Attribute("desc");
      pProp = new CMFCPropertyGridFileProperty(sName,_T(".\\"),NULL,sDesc);
      iData = 0;
      elem->Attribute("disabled",&iData);
	  pProp->Enable(!iData);
	 }
	 else
	 {
      sDesc = elem->Attribute("desc");
      sValue = elem->Attribute("value");
      sValueType = elem->Attribute("vtype");
      sFormat = elem->Attribute("floatformat");
      if(sFormat == _T("")) sFormat = _T("%.2f");
      if(sValueType == _T("quaternion"))
      {
          pProp = new CExMFCPropertyGridProperty(sName,iData,TRUE, sFormat, TRUE);
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("W"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("X"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Y"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Z"),(float)0,_T(""),0,sFormat));
          iData = 0;
          elem->Attribute("disabled",&iData);
	      pProp->Enable(!iData);
      }
      else if(sValueType == _T("vector4"))
      {
          pProp = new CExMFCPropertyGridProperty(sName, iData, TRUE, sFormat, TRUE);
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("X"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Y"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Z"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("W"),(float)0,_T(""),0,sFormat));
          iData = 0;
          elem->Attribute("disabled",&iData);
	      pProp->Enable(!iData);
      }
      else if(sValueType == _T("vector2"))
      {
          pProp = new CExMFCPropertyGridProperty(sName, iData, TRUE, sFormat, TRUE);
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("X"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Y"),(float)0,_T(""),0,sFormat));
          iData = 0;
          elem->Attribute("disabled",&iData);
	      pProp->Enable(!iData);
      }
      else if(sValueType == _T("vector3"))
      {
          pProp = new CExMFCPropertyGridProperty(sName, iData, TRUE, sFormat, TRUE);
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("X"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Y"),(float)0,_T(""),0,sFormat));
          pProp->AddSubItem(new CExMFCPropertyGridProperty(_T("Z"),(float)0,_T(""),0,sFormat));
          iData = 0;
          elem->Attribute("disabled",&iData);
	      pProp->Enable(!iData);
      }
      else
      {
          if(sValueType == "float") sValue.ChangeType(VT_R4);
          else if(sValueType == "integer") sValue.ChangeType(VT_INT);
          else if(sValueType == "boolean") sValue.ChangeType(VT_BOOL);
          pProp = new CExMFCPropertyGridProperty(sName,sValue,sDesc,iData,sFormat);
          iData = 0;
          elem->Attribute("disabled",&iData);
	      pProp->Enable(!iData);
      }
	 }
     RecurseLoadProperty(pProp,elem);
	 if(!iValueList&&(sType == "1")) pProp->Expand(true);
     prop->AddSubItem(pProp);
	} while(elem = elem->NextSiblingElement());
}
//------------------------------------------------------------------------------------
void LoadPropSheet(CMFCPropertyGridProperty **prop, String file)
{
    TiXmlDocument doc( file.c_str() );
    if(doc.LoadFile())
    {
	    *prop = new CMFCPropertyGridProperty(_T("Object Properties"),BIND_MAIN,false);
	    TiXmlNode* node = doc.FirstChild("PROPERTYSHEET");
     	TiXmlElement* element = node->ToElement();
	    RecurseLoadProperty(*prop,element);
    }
}
//------------------------------------------------------------------------------------
CString QualifyPath(const String &dirname)
{
    TCHAR outstr[1000];
    CString sTemp(dirname.c_str());
    PathSearchAndQualify(sTemp,outstr,1000);
    return CString(outstr);
}
//------------------------------------------------------------------------------------
BOOL MakeDirectory(CString dirname)
{
//     if(_wmkdir(dirname) == 0) 
//         return TRUE;
    int errval;
    _get_errno(&errval);
    if(errval == ENOENT)
    {
        CString msg;
        msg.Format(_T("Error creating Directory %s!!"),dirname);
        MessageBox(NULL,msg,_T("Scene Builder"),MB_OK);
        return FALSE;
    }
    return TRUE;
}
//------------------------------------------------------------------------------------
void CleanPath(CString &path)
{
    int pos = 0;
    path.Replace('/','\\');
    pos = path.Find(_T("\\\\"),0);
    while(pos != -1)
    {
        path.Delete(pos,1);
        pos = path.Find(_T("\\\\"),0);
    }
    pos = path.Find(_T("\\.\\"),0);
    while(pos != -1)
    {
        path.Delete(pos,2);
        pos = path.Find(_T("\\.\\"),0);
    }
}
//------------------------------------------------------------------------------------
CString ExtractFileName(CString path)
{
	int iStart = 0;
	CString fName;
	while(path.Tokenize(_T("\\"),iStart) != _T(""))
	{
        fName = path;
        path.Delete(0,iStart);
        iStart = 0;
	}
	return fName;
}
//------------------------------------------------------------------------------------
CString ExtractFilePath(CString filename)
{
	int iStart = 0;
	CString path = filename;
	CString fName;
	while(path.Tokenize(_T("\\"),iStart) != _T(""))
	{
	   fName = path;
	   path.Delete(0,iStart);
       iStart = 0;
	}
	filename.Delete(filename.Find(fName,0),fName.GetLength());
	return filename;
}
//------------------------------------------------------------------------------------
String ConvertToApoString(CString val)
{
    char nstring[1000];
    size_t nt;
    //WideCharToMultiByte( CP_ACP, 0, val, -1, nstring, 1000, NULL, NULL );
    //wcstombs_s(&nt,nstring,1000,val,_TRUNCATE);
    return String(nstring);
}
//------------------------------------------------------------------------------------
CString GetRelativePath(const CString pathFrom,const CString pathTo, bool isfile, CString PathToModifier)
{
   TCHAR outstr[1000];
   CString modifiedPathFrom = pathFrom;
   CString modifiedPathTo = pathTo;

   if(modifiedPathFrom.Find(_T(".")) == 0)
   {
       GetModuleFileName(NULL,outstr,1000);
       modifiedPathFrom = ExtractFilePath(outstr) + _T("\\") + pathFrom;
       PathSearchAndQualify(modifiedPathFrom,outstr,1000);
       modifiedPathFrom = outstr;
   }
   if(modifiedPathTo.Find(_T(".")) == 0)
   {
       modifiedPathTo = PathToModifier + _T("\\") + pathTo;
       PathSearchAndQualify(modifiedPathTo,outstr,1000);
       modifiedPathTo = outstr;
   }
   if(PathRelativePathTo(outstr,modifiedPathFrom,FILE_ATTRIBUTE_DIRECTORY,modifiedPathTo,isfile ? FILE_ATTRIBUTE_NORMAL:FILE_ATTRIBUTE_DIRECTORY))
       return outstr;
   else
       return pathTo;
}
//------------------------------------------------------------------------------------
BOOL CopyFilesEx(CString source, CString destination)
{
    SHFILEOPSTRUCT SHFileOp;
    TCHAR strSource[MAX_PATH + 2];
    TCHAR strDest[MAX_PATH + 2];

    _tcscpy_s(strSource,MAX_PATH + 2,source);
    _tcscpy_s(strDest,MAX_PATH + 2,destination);
    strSource[source.GetLength() + 1] = 0;
    strDest[destination.GetLength() + 1] = 0;

    //	set up File Operation structure
    ZeroMemory(&SHFileOp, sizeof(SHFILEOPSTRUCT));
    SHFileOp.hwnd = NULL;
    SHFileOp.wFunc = FO_COPY;
    SHFileOp.pFrom = strSource;
    SHFileOp.pTo = strDest;
    SHFileOp.fFlags = FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION;

    if(SHFileOperation(&SHFileOp) != 0) 
        return false;
    return true;
}
//------------------------------------------------------------------------------------
